package com.zcx.project.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zcx.atom.util.UserUtil;
import com.zcx.project.entity.ZcxUser;
import com.zcx.project.mapper.ZcxUserMapper;


@Controller
@RequestMapping("/TLogin")
public class IndexController {


    @Autowired
    ZcxUserMapper ZcxUserMapper;


    @ResponseBody
    @RequestMapping(value = "/login")
    public Map<String, Object> adminLogin(ZcxUser ZcxUser, HttpSession session, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();

        ZcxUser tAdmin = ZcxUserMapper.login(ZcxUser);
        if (tAdmin != null) {
            if (tAdmin.getPassword().equals(ZcxUser.getPassword())) {

                session.setAttribute("user", tAdmin);
                UserUtil.login(tAdmin);
                map.put("user", tAdmin);
                map.put("isFlag", 0);
            } else {
                map.put("isFlag", 1);
            }
        } else {
            map.put("isFlag", 2);
        }

        return map;
    }


    @RequestMapping(value = "/home")
    public ModelAndView getHome(HttpSession session, HttpServletRequest request) {
        /**
         *
         */
        ModelAndView mv = new ModelAndView("home");

        return mv;
    }

    @RequestMapping(value = "/index")
    public String getIndex(HttpSession session, HttpServletRequest request) {
        return "login";
    }

    @RequestMapping(value = "/main")
    public String getMainPage(HttpSession session, HttpServletRequest request) {
        return "index";
    }


    @RequestMapping(value = "/loginout")
    public String loginout(HttpSession session, HttpServletRequest request) {
        session.removeAttribute("user");
//    	UserUtil.logout();
        return "login";
    }


    @ResponseBody
    @RequestMapping(value = "/editPassword")
    public Map<String, Object> updateTLogin(ZcxUser obj) {
        int i = ZcxUserMapper.updateByPrimaryKeySelective(obj);
//		UserUtil.logout();
//		UserUtil.login(obj);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "修改失败！");
        else
            returnMap.put("showInfo", "修改成功！请重新登录");
        return returnMap;
    }


    @RequestMapping(value = "/userLogin")
    public ModelAndView userLogin(HttpSession session, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("user/shop-login");

        return mv;

    }

}
